
from bs4 import BeautifulSoup
import os

model_path = os.path.dirname(os.path.abspath(__file__))

def read_style_html():
    """读取 style.html 文件内容
    
    Returns:
        str: HTML 文件内容
    """
    style_file = os.path.join(model_path, "style.html")
    with open(style_file, "r", encoding="utf-8") as f:
        return f.read()

def gen_prodes(item:dict)->str:
    """
    根据商品信息,生成商品详情
    """
    html_str = f"""
    <div class="cxsw_description">
    <h1>商品详细信息</h1>
    <table class="product-table">
        <tr>
        <th>商品名称</th>
        <td id="product-name">{item.get("name")}</td>
        </tr>
        <tr>
        <th>商品品牌</th>
        <td id="product-brand">皆为尔(GENVIEW)</td>
        </tr>
        <tr>
        <th>货号</th>
        <td id="product-code">{item.get("productNum")}</td>
        </tr>
        <tr>
        <th>规格</th>
        <td id="product-spec">{item.get("specification")}</td>
        </tr>
        <tr>
        <th>CAS号</th>
        <td id="product-cas">{item.get("casNo","")}</td>
        </tr>
    </table>
    </div>
    """

    soup = BeautifulSoup(html_str, "html.parser")
    style_html = read_style_html()
    soup.insert(0, BeautifulSoup(style_html, "html.parser"))
    return str(soup)